﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 股票数据;

namespace StockDataBase.DataOperation
{
    class FinanceDataOp : DatabaseOp
    {
        public FinanceDataOp()
        {
            ConnectToDatabase(Utils.DB_PATH);
            CreateTable();
        }


        public void InsertTable(string stockId, string dateTime, Dictionary<string, Int64> dic, string table)
        {
            string sql = @"replace into " + table + " ( StockId,DateTime,";
            foreach (var item in dic)
            {
                sql += ("[" + item.Key + "],");
            }
            sql = sql.Remove(sql.Length - 1, 1);
            sql += ") values ('" + stockId + "','" + dateTime + "',";
            foreach (var item in dic)
            {
                sql += (item.Value + ",");
            }
            sql = sql.Remove(sql.Length - 1, 1);
            sql += ") ";

            this.ExcuteSql(sql);

        }

        public void CreateTable()
        {
            string asset = @"CREATE TABLE IF NOT EXISTS 'asset' (
	                            [StockId]   TEXT  ,
	                            [DateTime]   TEXT  ,
	                            [非流动资产] INTEGER,                        
	                            [流动资产] INTEGER,
	                            [净流动资产] INTEGER,
	                            [流动负债] INTEGER,
	                            [非流动负债] INTEGER,
	                            [少数股东权益-借/贷] INTEGER,
	                            [净资产/负债] INTEGER,
	                            [股本(资本及储备)] INTEGER,
	                            [储备(资本及储备)] INTEGER,
	                            [股东权益/亏损(合计)] INTEGER,
	                            [无形资产(非流动资产)] INTEGER,
	                            [物业、厂房及设备(非流动资产)] INTEGER,
	                            [附属公司权益(非流动资产)] INTEGER,
	                            [联营公司权益 (非流动资产)] INTEGER,
	                            [其他投资(非流动资产)] INTEGER,
	                            [应收账款(流动资产)] INTEGER,
	                            [存货(流动资产)] INTEGER,
	                            [现金及银行结存(流动资产)] INTEGER,
	                            [应付帐款(流动负债)] INTEGER,
	                            [银行贷款(流动负债)] INTEGER,
	                            [非流动银行贷款(非流动负债)] INTEGER,
	                            [总资产] INTEGER,
	                            [总负债] INTEGER,
	                            [股份数目] INTEGER
                            );";
            string cash = @"CREATE TABLE IF NOT EXISTS 'cash' (
	                            [DateTime]   TEXT  ,
	                            [StockId]   TEXT  ,
	                            [经营业务所得之现金流入净额] INTEGER,
	                            [投资活动之现金流入净额] INTEGER,
	                            [融资活动之现金流入净额] INTEGER,
	                            [现金及现金等价物增加] INTEGER,
	                            [会计年初之现金及现金等价物] INTEGER,
	                            [会计年终之现金及现金等价物] INTEGER,
	                            [外汇兑换率变动之影响] INTEGER,
	                            [购置固定资产款项] INTEGER
                            );";

            string profit = @"CREATE TABLE IF NOT EXISTS 'profit' (
	                            [DateTime]   TEXT  ,
	                            [StockId]   TEXT  ,
	                            [营业额] INTEGER,
	                            [税项] INTEGER,
	                            [除税前盈利] INTEGER,
	                            [除税后盈利] INTEGER,
	                            [少数股东权益] INTEGER,
	                            [股息] INTEGER,
	                            [股东应占盈利/(亏损)] INTEGER,
	                            [除税及股息后盈利/(亏损)] INTEGER,
	                            [基本每股盈利(元)] INTEGER,
	                            [摊薄每股盈利(元)] INTEGER,
	                            [每股股息(元)] INTEGER,
	                            [销售成本] INTEGER,
	                            [折旧] INTEGER,
	                            [销售及分销费用] INTEGER,
	                            [一般及行政费用] INTEGER,
	                            [利息费用/融资成本] INTEGER,
	                            [毛利] INTEGER,
	                            [经营盈利] INTEGER,
	                            [应占联营公司盈利/(亏损)] INTEGER
                            )";

            string asset_bank = @"CREATE TABLE IF NOT EXISTS 'asset_bank' (
                                    [DateTime] TEXT  ,
	                                [StockId]   TEXT   ,
	                                [现金及短期资金] INTEGER,
	                                [银行同业及其他金融机构之存款] INTEGER,
	                                [贸易票据] INTEGER,
	                                [持有的存款证] INTEGER,
	                                [香港特区政府负债证明书] INTEGER,
	                                [客户贷款及其他帐项] INTEGER,
	                                [持作买卖用途的证据] INTEGER,
	                                [持至到期证券债务证券股票] INTEGER,
	                                [联营公司权益] INTEGER,
	                                [合资公司权益及其他权益] INTEGER,
	                                [无形资产] INTEGER,
	                                [固定资产及投资物业] INTEGER,
	                                [其他资产] INTEGER,
	                                [总资产] INTEGER,
	                                [香港特区政府纸币流通额] INTEGER,
	                                [银行及其他金融机构存款及结余] INTEGER,
	                                [客户存款] INTEGER,
	                                [已发行的存款证] INTEGER,
	                                [已发行的债务证券] INTEGER,
	                                [其他负债] INTEGER,
	                                [总负债] INTEGER,
	                                [借入资本] INTEGER,
	                                [股本] INTEGER,
	                                [储备] INTEGER,
	                                [股东权益/亏损] INTEGER,
	                                [少数股东权益-借/贷] INTEGER,
	                                [资本来源合计] INTEGER,
	                                [股份数目] INTEGER
                                );";

            string profit_bank = @"CREATE TABLE IF NOT EXISTS 'profit_bank' (
	                                [DateTime]   TEXT  ,
	                                [StockId]   TEXT  ,
	                                [利息收入] INTEGER,
	                                [利息支出] INTEGER,
	                                [净利息收入] INTEGER,
	                                [其他经营收入] INTEGER,
	                                [总经营收入] INTEGER,
	                                [总经营支出] INTEGER,
	                                [总预备] INTEGER,
	                                [其他营利/亏损] INTEGER,
	                                [税项] INTEGER,
	                                [除税前盈利/亏损] INTEGER,
	                                [除税后盈利/亏损] INTEGER,
	                                [少数股东权益] INTEGER,
	                                [股东应占盈利/亏损] INTEGER,
	                                [股息] INTEGER,
	                                [除税及股息后盈利/亏损] INTEGER,
	                                [基本每股营利] INTEGER,
	                                [摊薄每股盈利] INTEGER,
	                                [每股股息] INTEGER
                                );";

            this.ExcuteSql(asset);
            this.ExcuteSql(asset_bank);
            this.ExcuteSql(cash);
            this.ExcuteSql(profit);
            this.ExcuteSql(profit_bank);

        }


    }

}
